package com.ruoyi.api;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.biz.domain.BizClass;
import com.ruoyi.biz.domain.BizSubmitWork;
import com.ruoyi.biz.domain.BizWork;
import com.ruoyi.biz.mapper.BizClassMapper;
import com.ruoyi.biz.mapper.BizSubmitWorkMapper;
import com.ruoyi.biz.mapper.BizWorkMapper;
import com.ruoyi.biz.service.IBizSubmitWorkService;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/work")
@Slf4j
public class ApiWorkController {

    @Resource
    private BizWorkMapper workMapper;

    @Resource
    private BizSubmitWorkMapper submitWorkMapper;

    @Resource
    private BizClassMapper classMapper;

    @Resource
    IBizSubmitWorkService submitWorkService;

    /*
    * 根据课程id查询作业列表
    * */
    @GetMapping("/list")
    public AjaxResult list(Long courseId){
        log.info("根据课程id：{}，查询作业列表",courseId);
        List<BizWork> bizWorks = workMapper.selectByCourseId(courseId);
        BizClass bizClass = classMapper.selectByCourseId(courseId);
        AjaxResult success = AjaxResult.success();
        success.put("works",bizWorks);
        success.put("class",bizClass);
        return success;
    }

    /*
    * 提交作业
    * */
    @PostMapping("/submit")
    public AjaxResult submitWork(@RequestBody BizSubmitWork submitWork){
        log.info("提交作业：{}",submitWork);
        submitWork.setStudentId(StpUtil.getLoginIdAsLong());
        submitWorkService.insertBizSubmitWork(submitWork);
        return AjaxResult.success();
    }


    /*
    * 查询作业提交和作业信息
    * */
    @GetMapping("/info")
    public AjaxResult getInfo(Long workId){
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        BizSubmitWork bizSubmitWork = submitWorkMapper.selectByWorkIdAndStudentId(workId, loginIdAsLong);
        BizWork bizWork = workMapper.selectBizWorkById(workId);
        AjaxResult success = AjaxResult.success();
        success.put("submit",bizSubmitWork);
        success.put("work",bizWork);

        return success;
    }
}
